/***************************************************************************
 *   Copyright (C) 2009-2010 by Lasse Liehu                                *
 *   lliehu@kolumbus.fi                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef LOGINOPERATION_H
#define LOGINOPERATION_H

#include <QObject>

class GoogleLoginData;
class LogWidget;
class LoginOperationPrivate;

namespace Login
{
    enum Status { Initializing, RequestSent, ResponseHeaderReceived,
                  ReceivingResponseData, ResponseDataReceived, ParsingData,
                  Completed };
    enum Conclusion { InternalError, UnknownError, ConnectionError,
                      UnknownServerError, NoSidInDataError, AuthenticationError,
                      SthNeedsFixingError, SslError, Success };
}

/**
  * @note LoginOperations don't destroy themselves anymore. Someone should
  * destroy them, probably the one that created them in the first place.
  */
class LoginOperation : public QObject
{
    Q_OBJECT
    public:
        explicit LoginOperation(GoogleLoginData& loginData, QObject *parent = 0);
        virtual ~LoginOperation();
    signals:
        /**
          * This signal should be emitted when the status of the login
          * operation changes. When the operation has finished whatever it was
          * doing, whether successfully, whether an error happened, whether it
          * was cancelled, still Status Completed should always be emitted.
          * @attention The status Completed should be set after everything else,
          *            like finalizing, cleaning up, even after emitting
          *            setConclusion. Setting that status means that the object
          *            is ready to be destroyed as soon as the app gets back to
          *            its event loop.
          * @see setConclusion(Conclusion, const QString&)
          */
        void updateStatus(Login::Status);
        /**
          * This signal is used to tell whether login succeeded or failed. This
          * should be called before emitting updateStatus() to change status to
          * Completed. @c details can be used to give some extra information. It's
          * not directly shown to the user, but it might be available when user
          * clicks "More..." or "Details". That's why the message shouldn't be
          * like an obvious debug message. Also the message doesn't have to be
          * extra short, but it can't be like one long paragraph, either. :)
          * @see updateStatus(Status)
          */
        void setConclusion(Login::Conclusion, const QString& details);
    public slots:
        virtual void start() = 0;
    protected:
        /**
         * This is a getter for GoogleLoginData.
         */
        GoogleLoginData& loginData();
    private:
        LoginOperationPrivate *const bd;

        static bool enumTypesRegistered;
};

#endif // LOGINOPERATION_H
